package cn.haut.online_read.service;

import cn.haut.online_read.enrity.User;
import cn.haut.online_read.mapper.UserMapper;
import cn.haut.online_read.utils.ComputeDigest;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xing
 * @date 2021-05-12 20:56
 */
@Service("userService")
public class UserService {

    private UserMapper userMapper;
    /**
     * 验证用户登录返回给Controller 值
     */
//    public static final int VALIDATE_USER_PASS = 1;
    public static final int VALIDATE_USER_NOT_PASS = 0;
    public static final int VALIDATE_USER_NOT_FOUND = -1;

    @Autowired
    public void setUserMapper(UserMapper userMapper){
        this.userMapper = userMapper;
    }

    /**
     *  验证用户
     *
     * @param username
     * @param password
     * @return 1: 验证通过，0: 密码错误，-1: 查无此人
     */
    public int validateUser(String username, String password) {
        User user = userMapper.getUserByName(username);
        // 查询结果为空，查无此人
        if(user == null) {
            return VALIDATE_USER_NOT_FOUND;
        }
        // 密码相等， 验证通过
        else if(ComputeDigest.sameAfterDigest(password, user.getPassword())){
            // 用户权限为 1 或者 2
            return user.getPermission();
        }
        // 验证失败
        return VALIDATE_USER_NOT_PASS;
    }


    /**
     * 注册用户
     *
     * @param username
     * @param password
     * @return
     */
    public boolean registerUser(String username, String password) {
        User user = userMapper.getUserByName(username);
        if(user != null) {
            return false;
        } else {
            Boolean register = userMapper.registerUser(username, ComputeDigest.digest(password));
            return register != null && register;
        }
    }

    /**
     * 根究用户名获取权限
     *
     * @return
     */
    public int getPermissionByName(String username) {
        User userByName = userMapper.getUserByName(username);
        if(userByName != null) {
            return userByName.getPermission();
        }
        return UserService.VALIDATE_USER_NOT_PASS;
    }

    /**
     * 获取所用普通用户信息
     *
     * @return
     */
    public List<String> getAllUsers() {
        return userMapper.getAllUsers();
    }

    /**
     * 修改密码
     *
     * @param username
     * @param newPassword
     * @return
     */
    public boolean changePassword(String username, String newPassword) {
        Integer setPassword = userMapper.setPassword(username, ComputeDigest.digest(newPassword));
        return setPassword != null && setPassword == 1;
    }

    /**
     * 删除用户
     *
     * @param username
     * @return
     */
    public boolean deleteUser(String username) {
        // TODO 可以使用触发器删除
        // 删除包含用户外键的书架
        Integer deleteShelf = userMapper.deleteUserShelf(username);
        if(deleteShelf != null) {
            Integer deleteUser = userMapper.deleteUser(username);
            return deleteUser != null;
        }
        return false;
    }

    /**
     * 查找用户
     *
     * @param searchUsername
     * @return
     */
    public List<String> getUserByUsername(String searchUsername) {
        return userMapper.selectUserByUsername(searchUsername);
    }

    /**
     * 获取所有用户的总数
     *
     * @return
     */
    public int getAllUsersCount() {
        return userMapper.getAllUsersCount();
    }

    /**
     * 通过分页获取用户信息
     *
     * @return
     */
    public List<String> getUsersByPagination(int pageSize, int currentPage) {
        int offset = (currentPage - 1) * pageSize;
        return userMapper.getUsersByPagination(offset, pageSize);
    }

    /**
     * 获取用户信息 通过 用户名 近似查找， 以及 分页
     *
     * @param searchUsername
     * @param pageSize
     * @param currentPage
     * @return
     */
    public List<String> searchUserByPagination(String searchUsername, int pageSize, int currentPage) {
        int offset = (currentPage - 1) * pageSize;
        return userMapper.searchUsersByPagination(searchUsername, offset, pageSize);
    }

    /**
     * 获取近似查询到的所有用户的数量
     *
     * @return
     */
    public int getAllUsersCountByUsername(String searchUsername) {
        return userMapper.searchUsersCount(searchUsername);
    }
}
